package com.hdsx.netty.tomcatserver.netty;

import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.*;
import io.netty.util.CharsetUtil;

/**
 * @author 占贤银
 * time 2024/8/5 23:34
 * description 生命之灯因热情而点燃，生命之舟因拼搏而前行
 */
public class XYResponse {

    private ChannelHandlerContext ctx;
    private HttpRequest req;

    public XYResponse(ChannelHandlerContext ctx, HttpRequest req) {
        this.ctx = ctx;
        this.req = req;
    }

    public void write(String content) {
        if (content == null || content.length() == 0) {
            return ;
        }
        FullHttpResponse response =
                new DefaultFullHttpResponse
                        (HttpVersion.HTTP_1_1,
                                HttpResponseStatus.OK,
                                Unpooled.wrappedBuffer(content.getBytes(CharsetUtil.UTF_8)));
        response.headers().set("Content-Type", "text/html;charset=utf-8");
        ctx.write(response);
        ctx.flush();
        ctx.close();
    }
}
